authApp.value('page','material_apply'); 
authApp.controller("applyMController",function($scope,$http,$location,$getQueryString,$splice){
	$scope.preNum=$getQueryString("preNum");
	$scope.isAddM=true;
	$scope.showEdit = false;
	var requestJson={'pageNumber':'0','pageSize':'10','fid':$getQueryString("pid"),'type':'2'};
	$scope.addM=function(){
		$scope.isAddM=false;
	}
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.addFZ="";
		$scope.addFZNum="";
	}
	$scope.addFirstZ=function(){
		if($scope.addFZ){
			var rJson={'content':$scope.addFZ,'fid':$getQueryString("pid"),'type':'2','number':$scope.addFZNum,'major':$getQueryString("major")};
			$http({
				method:'post',
				url:apis.insertItem,
				data:rJson,
			}).success(function(response){
				var radom=Math.random();
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.getAML();
					$scope.addFZ="";
					$scope.addFZNum="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}else{
			alert("请输入二级指标");
		}
	}
	//编辑
	$scope.edit = function (item) {
        $scope.item = item;
        $scope.showEdit = true;
    }
    $scope.doEdit = function (item) {
        if(!item.number){
            alert("请填写序号");
            return;
        }
        if(!item.content) {
            alert("请输入二级指标标题");
            return;
        }
        var rJson={'content':item.content,'number':item.number,'pid':item.pid};
        $http({
            method:'post',
            url:apis.updateItem,
            data:rJson,
        }).success(function(response){
            if(response.code=="000000"){
                alert("编辑成功");
                $scope.showEdit=false;
                $scope.getAML();
                $scope.item="";
            }else{
                alert(response.errorMsg);
            }
        }).error(function(){
            alert("服务器打瞌睡了，请稍等！");
            $scope.showEdit=false;
        })
    }
    $scope.doCancel = function () {
        $scope.showEdit=false;
    }
	$scope.getAML=function(){
		$http({
			method:'post',
			url:apis.queryItem,
			data:requestJson,
		}).success(function(response){
			if(response.code=="000000"){
				$scope.list=response.data.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	$scope.delCollect=function(pid){
		if(window.confirm("是否确定删除？")){
			var rJson={'pid':pid};
			$http({
				method:'post',
				url:apis.deleteItem,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.getAML();
				}else{
					alert(response.errorMsg);
				}
			})
		}
	}
	$scope.getAML();
})




authApp.directive('observe', function(){
    return {
        restrice: 'EA',
        controller: function($scope, pagerConfig){
            // 共多少条
            $scope.totalItems = 0;
            $scope.page = [];
            // 偏移数
            $scope.offsetPage = 0;
            // 一页多少条
            $scope.itemsPerpage = 0;
            // 一个多少页
            $scope.totalPages = 0;
            $scope.currentPage = 0;

            $scope.$watch('totalItems', function(){
                $scope.totalPages = Math.ceil($scope.totalItems / $scope.itemsPerpage);

                resetPageList();
                if ($scope.page[$scope.currentPage]) {
                    $scope.page[$scope.currentPage].active = true;
                }
            });

            var resetPageList = function(){
                $scope.page = [];

                var last = Math.min(Number($scope.offsetPage) + Number($scope.listSizes), $scope.totalPages);

                for (var i = $scope.offsetPage; i < last; i ++) {
                    $scope.page.push({
                        text: i,
                        indexPage: i,
                        active: false
                    })
                }

            }
            var getOffset = function(index){
                var offset = Math.min(index, $scope.totalPages - $scope.listSizes);
                if (offset <= 0) {
                    offset = 0;
                }
                return offset;
            };
            $scope.selectPage = function(index){
                if (index < 0 || index >= $scope.totalPages) {
                    return;
                }
                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = false;
                }
                $scope.currentPage = index;
                // 如果currentPage 小于 offsetPage 或者 currentPage 大于 offsetPage加listsizes

                if ($scope.currentPage < $scope.offsetPage || $scope.currentPage >= $scope.offsetPage + $scope.page.length) {

                    $scope.offsetPage = getOffset(index)

                    resetPageList();
                }

                if ($scope.page[$scope.currentPage-$scope.offsetPage]) {
                    $scope.page[$scope.currentPage-$scope.offsetPage].active = true;
                }
                $scope.$emit('pagechage', $scope.currentPage);
            };
            $scope.next = function(){
                if ($scope.isLast()) {
                    return;
                }
                $scope.selectPage($scope.currentPage + 1);
            };
            $scope.provie = function(){
                if ($scope.isFirst()) return
                $scope.selectPage($scope.currentPage - 1);
            }
            $scope.first = function(){
                $scope.selectPage(0);
            }
            $scope.last = function(){
                $scope.selectPage($scope.totalPages - 1);
            }
            $scope.isFirst = function(){
                return $scope.currentPage <= 0;
            };
            $scope.isLast = function(){
                return $scope.currentPage >= $scope.totalPages - 1;
            }
            $scope.getText = function(key) {
                return pagerConfig.text[key];
            };


        },
        link: function(scope, ele, attrs){

            scope.itemsPerpage = attrs.itemsperpage || 1;
            scope.listSizes = attrs.listsizes;
            attrs.$observe('totalitems', function(val){
                scope.totalItems = val;
            })
        },
        templateUrl: '../../views/modal/page.html'
    }
}).constant('pagerConfig', {
        text: {
            'first': '首页',
            'provie': '上一页',
            'next': '下一页',
            'last': '尾页',
        }
    }
);
	